<?php

class mod_mblog 
{
	
	//抓取新转发
	function get_new_rt_count_by_mid($mid,$start_id="",$p="")
	{
		if($p < 1) $p=1;
		
		$db_base = new db_base();
		if(!$start_id && $p ==1)
		{
			//开始抓取的id
			$db_mblog_rt_count= new db_mblog_rt_count();
			$mid_info = $db_mblog_rt_count->cron_get_max_status_id_by_mid($mid);
			if($mid_info)$start_id = $mid_info["status_id"];
		}
	
		
		$sql =" insert ignore into  mblog_rt_count (mid,status_id,status) values ";
		$sql_array = array();
		
		$mblog_from_api = new mblog_from_api();
		
		$mblog_list = $mblog_from_api->api_get_rt_ids_by_mid($mid,$start_id,"",$p,200);
		$mblog_list = $mblog_list["data"]["result"];
		
		foreach($mblog_list as $value)
		{
			$sql_array[] = " ($mid,$value,1) ";
		}
		
		$db_mblog_rt_mid= new db_mblog_rt_mid();
		if($sql_array)
		{
			$sql .= join(",",$sql_array);
			$res = $db_base->query($sql);
		}
		
		//如果个数大于150就算没有结束，继续抓下一页
		
		if(count($mblog_list) > 150)
		{
			$p = $p+1;
			$this->get_new_rt_count_by_mid($mid,$start_id,$p);
		}
		
			
	}
	
	
	//更新具体转发数
	function update_rt_count_by_mid($mid)
	{
		$db_mblog_rt_count = new db_mblog_rt_count();
		$rt_count = $db_mblog_rt_count->cron_get_count_by_mid($mid);
		$rt_page = ceil($rt_count/100);//100一页
		
		if($rt_page > 10) $rt_page =10;//做多更新1W条。不然会超时
		
		for($i=1;$i<=$rt_page;$i++)
		{
			$this->update_rt_count_by_page($mid,$i); 
		}
			
	}
	
	//更新具体转发数
	function update_rt_count_by_page($mid,$p="")
	{
		if($p < 1) $p=1;
		
		$db_base = new db_base();
		$db_mblog_rt_count= new db_mblog_rt_count();
		$rt_list = $db_mblog_rt_count->cron_get_list_by_mid_order_by_rt_num($mid,$p,100);
		
		$status_id_array = array();
		$res = array();
		foreach($rt_list as $key => $value)
		{
			$status_id_array[] = $value["status_id"];
			$res["{$value["status_id"]}"] = array("reposts_count"=>$value["reposts_count"],"comments_count"=>$value["comments_count"]);
		}
		
		$mid = join(",",$status_id_array);
		$mblog_from_api = new mblog_from_api();
		$mblog_list = $mblog_from_api->api_get_mblog_counts_by_mids($mid,900);
	
		foreach($mblog_list["data"]["result"] as $key => $value)
		{
			if($value["comments"] != $res["$key"]["comments_count"] || $value["reposts"] != $res["$key"]["reposts_count"])
			{	
				$db_mblog_rt_count->cron_update_count_by_status_id($key,$value["comments"],$value["reposts"]);
			}
			
		}
		
			
	}
	
	
	
	function get_topic_list_order_by_like($pcount=10)
	{
	
		return ;
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		//类型1
		$list1 = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_list_order_by_like(1,1,$pcount);
		
		$list2 = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_list_order_by_like(2,1,$pcount);
		
		$list3 = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_list_order_by_like(3,1,$pcount);
		
		$list4 = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_list_order_by_like(4,1,$pcount);
		
		return array($list1,$list2,$list3,$list4);
	
	
	}
	
	function get_mblog_cron_topic_tag_list_order_by_count_want($pcount=10)
	{
	
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		//类型1
		$list1 = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_list_order_by_count_want(1,1,$pcount);
		
		$list2 = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_list_order_by_count_want(2,1,$pcount);
		
		$list3 = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_list_order_by_count_want(3,1,$pcount);
		
		//$list4 = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_list_order_by_count_want(4,1,$pcount);
		
		return array($list1,$list2,$list3,$list4);
	
	
	}
	
	
	function get_mblog_cron_topic_tag_list_order_by_count_gonglue($pcount=10)
	{
	
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		//类型1
		$list1 = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_list_order_by_count_gonglue(1,1,$pcount);
		
		$list2 = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_list_order_by_count_gonglue(2,1,$pcount);
		
		$list3 = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_list_order_by_count_gonglue(3,1,$pcount);
		
		//$list4 = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_list_order_by_count_want(4,1,$pcount);
		
		return array($list1,$list2,$list3,$list4);
	
	
	}
	
	
	
	function add_mblog_to_album($item,$album_id,$status_id,$status_count)
	{
		$db_album_info = new db_album_info();
		$db_album_to_mblog = new db_album_to_mblog();
		
		$now = date("Y-m-d H:i:s");
		$res = $db_album_to_mblog->insert_album_to_mblog($album_id,$status_id,$now);
		
		if($res)
		{
			if(!$item["pic1"] || !$item["pic2"] || !$item["pic3"] || !$item["pic4"])
			{
				$db_mblog_meinv_content = new db_mblog_meinv_content();
				$mblog_info = $db_mblog_meinv_content->get_mblog_pic_content_item($status_id);
				$pic_url  = $mblog_info["pic"];
				
				if(!$item["pic1"])
				{
					$pic1 = $pic_url;
					$id1=$status_id;
				}
				elseif(!$item["pic2"])
				{
					$pic2 = $pic_url;
					$id2=$status_id;
				}
				elseif(!$item["pic3"])
				{
					$id3=$status_id;
					$pic3 = $pic_url;
				}
				elseif(!$item["pic4"])
				{
					$id4=$status_id;
					$pic4 = $pic_url;
				}
				else
				{
					$pic2 = $pic_url;
					$id2=$status_id;
				}
				
			}
			$status_count = $status_count + 1;
			$db_album_info->update_album_info_by_id($album_id,$uid,$title="",$desc="",$pic1,$pic2,$pic3,$pic4,$status_count,$status="",$show_type="",$body_url="",$body_attach="",$id1,$id2,$id3,$id4);
		}
	
		return $res;
	}
	
	
	function get_meinv_pubu_info($p=1,$pcount=30,$type="",$topic_id="",$uid="",$order="")
	{
		global $config;
		$db_mblog_meinv_content = new db_mblog_meinv_content();
		$db_mblog_user_love = new db_mblog_user_love();
		$db_mblog_video_content = new db_mblog_video_content();
		if(!$type)$type="common";
		/*if(!$topic_id)*/ $show_mblog_tag = true;
		$default_type = "mblog";
		$video_icon ="";
		
		$mod_user_info = new user_info();
		$current_uid = $mod_user_info->get_cur_uid();
		$check_uid = user_info::check_super_admin();
		
		if($check_uid || ($uid && $current_uid == $uid))
		{
			$show_del = 1;
		}
		
		$domain_url = "http://".$config["main_domain"];
		
		
		$list = $db_mblog_meinv_content->get_mblog_pubu_list_by_topic($p,$pcount,$topic_id,$uid);
		$count = $db_mblog_meinv_content->get_mblog_pubu_count_by_topic($topic_id,$uid);
		
		
		$db_user_info = new db_user_info();
		
		foreach($list as $key => $value)
		{
		
			$title_content = preg_replace("/<.*?>/","",$value["content"]);
			
			//根据类型判断链接地址
			
			if($type == "video")
			{
				$add_album ="";
				$add_like="";
				$value["pic"] = $value["video_pic"];
			}
			elseif($type =="mblog")
			{
				$value["pic"] =  str_replace("/bmiddle/","/mw220/",$value["pic"]);
			}
			
			if(!$show_del)
			{
				$add_comment="";
			}
			
			$value['create_time'] = TimeFormatter::timeFormatShort(strtotime($value['create_time']));
			
			if($show_mblog_tag && $value["topic"])
			{
				$show_mblog_content ="<a class='show-mblog-tag'  href='/meinv/index_{$value["topic_id"]}.html'>{$value["topic"]} </a> ";
			}
			else
			{
				$show_mblog_content="";
			}
			
			$pic_height = "";
			$class_long = "";
			$div_stop = "";
			if($value["pic_height_pubu"])
			{
				if($value["pic_height_pubu"] >= 800)
				{
					$class_long ="long";
					$div_stop='<span class="stop"></span>';
				}
				$pic_height = "height={$value["pic_height_pubu"]}";
			}
			else
			{
				$pic_height = "height=120";
			}
			
			
			
			$data = <<<EOF
					<div class="pin wfc " data-seq="{$value["mid"]}" data-id="{$value["id"]}" data-pic="{$value["pic"]}">
                        
                        <div class="actions">
                            <div class="right">
                                {$add_like}
                                {$add_comment}
                                
                            </div>
                            <div class="left">
                                {$add_album}
                            </div>
                             
                        </div> {$show_mblog_content}
                        <a class="img {$class_long} x" target='_blank' href="{$domain_url}/meinv/detail_{$value["id"]}.html">
                            <img width="192" {$pic_height} alt="{$title_content}" src="{$value["pic"]} ">
                            {$div_stop}
                        </a> 
                        <p class="description">
                           {$value["content"]} 
                        </p>
                        <p class="stats less">
                        </p>
                        <div class="convo attribution clearfix">
                            <p>
                              <a class="img  x" title="{$title_content}" href="/meinv/user_{$value["create_uid"]}.html">
                                    <img src="{$value["head_url"]}">
                                    
                                </a>
                                <a  href="/meinv/user_{$value["create_uid"]}.html">
                               {$value["create_nickname"]}
								</a>
								发表于{$value["create_time"]}
                                <a class="replyButton" title="回复">
                                </a>
                            </p>
                        </div>
                        <div class="comments muted" style="display:none;">
                        </div>
                        
                    </div>
EOF;
        
                               
                 $list[$key]["html"] = $data;
				unset($value);
		}
		
		
		
		
		return array("list"=>$list,"count"=>$count);
	
	}
	
	
	
	
	
	
	
	
	function get_pic_pubu_info($p=1,$pcount=30,$type="",$topic_id="",$show_type="",$uid="",$order="",$tag_id="")
	{
		global $config;
		
		if($topic_id)
		{
			
			$show_site_name = $config["current_site_name"]." ";
		}
		$db_mblog_pic_content = new db_mblog_pic_content();
		$db_mblog_user_love = new db_mblog_user_love();
		$db_mblog_video_content = new db_mblog_video_content();
		if(!$type)$type="common";
		/*if(!$topic_id)*/ $show_mblog_tag = true;
		$default_type = "mblog";
		$video_icon ="";
		
		$mod_user_info = new user_info();
		$current_uid = $mod_user_info->get_cur_uid();
		$check_uid = user_info::check_super_admin();
		
		if($check_uid || ($uid && $current_uid == $uid))
		{
			$show_del = 1;
		}
		
		if($type == "video")
		{
			$default_type = "video";
			$video_icon ='<img class="video-icon" src="/img/media_video.png">';
		}
		
		$default_link = "/{$default_type}/detail.php";
		if($topic_id)
		{
			$default_link = $default_link."?topic_id={$topic_id}&";
		}
		elseif($uid)
		{
			if($type == "common" || $type == "video" )
			{
				$default_link = $default_link."?uid={$uid}&";
			}
			
		}
		else 
		{
			$default_link =$default_link."?"; 
		}
		
		
		if($type == "common")
		{
			$list =  $db_mblog_pic_content->get_mblog_pubu_list_by_topic($p,$pcount,$topic_id,$uid,$tag_id);
			$count = $db_mblog_pic_content->get_mblog_pubu_count_by_topic($topic_id,$uid,$tag_id);
			
		}
		elseif($type == "video")
		{
			
			if($order == "time")
			{
				$list = $db_mblog_video_content->get_mblog_video_list_by_topic($p,$pcount,$topic_id,1,$show_type,$uid);
			}
			else
			{
				$list =  $db_mblog_video_content->get_mblog_video_list_order_by_count_tuijian($p,$pcount,$topic_id,1,$show_type,$uid);
			}
			
			$count = $db_mblog_video_content->get_mblog_video_count_by_topic($topic_id,1,$show_type,$uid);
		}
		elseif($type == "love")
		{
			$list = $db_mblog_user_love->get_mblog_user_love_content_list_by_uid($uid,$p,$pcount);
			$count = $db_mblog_user_love->get_mblog_user_love_count_by_uid($uid);
		}
		elseif($type == "att")
		{
			$db_user_follow = new db_user_follow();
			$follow_list = $db_user_follow->get_list_by_uid($uid);
			$uids_array = array();
			foreach($follow_list as $value)
			{
				$uids_array[] = $value["follow_uid"];
			}
			$uids = join(",",$uids_array);
			
			$list = $db_mblog_pic_content->get_mblog_pubu_list_by_topic($p,$pcount,"",1,"","",$uids);
			
		}
		
		
		foreach($list as $key => $value)
		{
		
			$domain = "http://".$config["domain_url"][$value["topic_id"]];
			if($config["current_site_id"] == 0)
			{
				$show_topic_name = $config["site_name"][$value["topic_id"]];
			}
			
			$title_content = preg_replace("/<.*?>/","",$value["content"]);
			$add_like = '<a class="like add_like btn btn11 wbtn" onclick="return false;" href="javascript:void(0);" ><strong><em></em>喜欢</strong><span></span></a>';
			
			$add_comment = '<a class="del_'.$type.' clickable  btn btn11 wbtn" onclick="return false;" href="javascript:void(0);" uid="'.$value["create_uid"].'" value="'.$value["id"].'"><strong><em></em> 删除</strong><span></span></a>';
				
				
			//$add_album = '<a class="add-album repin btn btn11 wbtn" href="javascript:void(0);" ><strong>专辑</strong><span></span></a>';
			
		
			//根据类型判断链接地址
			
			$value["pic"] =  str_replace("/bmiddle/","/mw220/",$value["pic"]);
			
			if(!$show_del)
			{
				$add_comment="";
			}
			
			$value['create_time'] = TimeFormatter::timeFormatShort(strtotime($value['create_time']));
			
			if($show_mblog_tag && $value["topic"])
			{
				$show_mblog_content ="<a class='show-mblog-tag' target='_blank' href='{$domain}/mblog/index_{$value["tag_id"]}.html'>{$show_topic_name}{$value["tag_name"]} </a> ";
			}
			else
			{
				$show_mblog_content="";
			}
			
			$pic_height = "";
			$class_long = "";
			$div_stop = "";
			if($value["pic_height_pubu"])
			{
				if($value["pic_height_pubu"] >= 800)
				{
					$class_long ="long";
					$div_stop='<span class="stop"></span>';
				}
				$pic_height = "height={$value["pic_height_pubu"]}";
			}
			else
			{
				$pic_height = "height=120";
			}
			
			
			
			$data = <<<EOF
					<div class="pin wfc " data-seq="{$value["mid"]}" data-id="{$value["id"]}" data-pic="{$value["pic"]}">
                        
                        <div class="actions">
                            <div class="right">
                                {$add_like}
                                {$add_comment}
                                
                            </div>
                            <div class="left">
                                {$add_album}
                            </div>
                             
                        </div> {$show_mblog_content}
                        <a class="img {$class_long} x" target='_blank' href="/mblog/detail_{$value["id"]}.html">
                            <img width="192" {$pic_height} alt="{$title_content}" src="{$value["pic"]} ">
                            {$div_stop}
                            {$video_icon}
                        </a> 
                        <p class="description">
                           {$value["content"]} 
                        </p>
                        <p class="stats less">
                        </p>
                        <div class="convo attribution clearfix">
                            <p>
                              <a class="img  x" title="{$title_content}" target='_blank' href="/user/{$value["create_uid"]}.html">
                                    <img src="{$value["head_url"]}">
                                    
                                </a>
                                <a target='_blank'href="/user/{$value["create_uid"]}.html">
                               {$value["create_nickname"]}
								</a>
								发表于{$value["create_time"]}
                                <a class="replyButton" title="回复">
                                </a>
                            </p>
                        </div>
                        
                    </div>
EOF;
        
                               
                 $list[$key]["html"] = $data;
				unset($value);
		}
		
		
		
		
		return array("list"=>$list,"count"=>$count);
	
	}
	
	
	
	
	function get_right_tuijian($site_id="",$create_time="")
	{
		global $config;
		
		$db_gonglue = new db_gonglue();
		$gonglue_list1 = $db_gonglue->get_list_order_by_time(1,6,0,$site_id,"",$create_time);
		if(count($gonglue_list1)<6)
		{
			$gonglue_list1 = $db_gonglue->get_list_order_by_time(1,6,0,$site_id,"","");
		}
		
		
		
		$gonglue_list2 = $db_gonglue->get_list_order_by_time(1,6,1,$site_id,"",$create_time);
		if(count($gonglue_list2)<6)
		{
			$gonglue_list2 = $db_gonglue->get_list_order_by_time(1,6,1,$site_id,"","",1);
		}
		
		
		foreach($gonglue_list1 as $key => $value)
		{
			if($value["topic_id"])
			{
				$gonglue_list1[$key]["topic_name"] = $config["site_name"][$value["topic_id"]];
				$gonglue_list1[$key]["domain_url"] = "http://".$config["domain_url"][$value["topic_id"]];
			}
			$gonglue_list1[$key]["title_short"] = string_utf8::substr_utf8_cn_v2($gonglue_list1[$key]["title"],36);
		}
		
		foreach($gonglue_list2 as $key => $value)
		{
			if($value["topic_id"])
			{
				$gonglue_list2[$key]["topic_name"] = $config["site_name"][$value["topic_id"]];
				$gonglue_list2[$key]["domain_url"] = "http://".$config["domain_url"][$value["topic_id"]];
			}
			$gonglue_list2[$key]["title_short"] = string_utf8::substr_utf8_cn_v2($gonglue_list2[$key]["title"],36);
		}
			
		
		$g_show["gonglue_list1"] = $gonglue_list1;
		$g_show["gonglue_list2"] = $gonglue_list2;
		
		$db_mblog_meinv_content = new db_mblog_meinv_content();
		$meinv_tuijian_list = $db_mblog_meinv_content->get_mblog_meinv_list_by_tuijian(1,9,"",$create_time);
		
		
		if(count($meinv_tuijian_list)<9)
		{
			$meinv_tuijian_list = $db_mblog_meinv_content->get_mblog_meinv_list_by_tuijian(1,9,"","",1);
		}
		
		
		foreach($meinv_tuijian_list as $key => $value)
		{
			$meinv_tuijian_list[$key]["pic_small"]  = str_replace("/bmiddle/","/thumb150/",$meinv_tuijian_list[$key]["pic"]);
		}
		
		$g_show["meinv_tuijian_list"] = $meinv_tuijian_list;
	
		return $g_show;
	}
	
	
}
